﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ece750project;
namespace ece750project
{
    public partial class AddNewPatients : Form
    {
        ComboBox cmp_Defualt_docref;
        public AddNewPatients()
        {
            InitializeComponent();
        }

        private void Fill_Defualt_doc()
        {
            
            DataTable DT;
            DT = DB_BusinessRules.get_Defualt_docs();
            defualt_doctor.Items.Clear();
            cmp_Defualt_docref.Items.Clear();
            string s;
            for (int i = 0; i <= DT.Rows.Count - 1; i++)
            {
                s = DT.Rows[i]["last_name"].ToString() + ", " + DT.Rows[i]["first_name"].ToString();
                int Person_ID = Convert.ToInt32(DT.Rows[i]["person_id"]);
                defualt_doctor.Items.Add(s);
                cmp_Defualt_docref.Items.Add(Person_ID);
            }

        }
        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void AddNewPatients_Load(object sender, EventArgs e)
        {
            cmp_Defualt_docref = new ComboBox();
            Fill_Defualt_doc();
            if (abc_app.ADDNewPatient_para.Show_mode == true)
            {
                Load_person_date(abc_app.ADDNewPatient_para.P_ID);
                clear.Enabled = false;
                confirm.Enabled = false;
                abc_app.ADDNewPatient_para.Show_mode = false;
            }
        }

        private void Load_person_date(int p_id)
        {
            DataTable DT = DB_BusinessRules.get_person(p_id);
            abc_ns.abc_class.person_Struct P = new abc_ns.abc_class.person_Struct();
            abc_ns.abc_class.person_Struct_Load(ref P, ref DT, 0);

            first_name.Text = P.first_name;
            last_name.Text = P.last_name;
            sex.SelectedIndex = P.sex;
            dob.Text = P.dob.ToShortDateString();
            sin.Text = P.ssn.ToString();
            healthcard.Text = P.health_no.ToString();
            insurance.Text = P.insurance_no.ToString();

            street_no.Text = P.street_no;
            street_name.Text = P.street_name;
            city.Text = P.city;
            province.Text = P.province;
            postal.Text = P.postal_code;
            country.Text = P.country;
            phone_no.Text = P.phone_number;
            emergency_phone.Text = P.phone_emergency;
            email.Text = P.email;
            defualt_doctor.SelectedIndex = cmp_Defualt_docref.Items.IndexOf(P.default_doctor);

        }
        private void clear_Click(object sender, EventArgs e)
        {
            first_name.Text = "";
            last_name.Text = "";
            sex.Text = "";
            dob.Text = "";
            sin.Text = "";
            healthcard.Text = "";
            insurance.Text = "";
            
            street_no.Text = "";
            street_name.Text = "";
            city.Text = "";
            province.Text = "";
            postal.Text = "";
            country.Text = "";
            phone_no.Text = "";
            emergency_phone.Text = "";
            email.Text = "";
            defualt_doctor.Text = "";
        }

        private void confirm_Click(object sender, EventArgs e)
        {
            abc_ns.abc_class.person_Struct Patient = new abc_ns.abc_class.person_Struct() ;

            Patient.city = city.Text;
            Patient.country = country.Text;
            Patient.default_doctor = 3; //Convert.ToInt32(defualt_doctor.Text);
            Patient.dob = Convert.ToDateTime(dob.Text);
            Patient.email = email.Text;
            Patient.first_name = first_name.Text;
            Patient.health_no = Convert.ToInt32( healthcard.Text);
            Patient.insurance_no = Convert.ToInt32( insurance.Text);
            Patient.last_name = last_name.Text;
            Patient.phone_emergency = emergency_phone.Text;
            Patient.phone_number = phone_no.Text;
            Patient.postal_code = postal.Text;
            Patient.province = province.Text;
            Patient.sex = sex.SelectedIndex;
            Patient.ssn = Convert.ToInt32(sin.Text);
            Patient.street_name = street_name.Text;
            Patient.street_no = street_no.Text;
            
            bool ret= DB_BusinessRules.Add_Patient(ref Patient);
            if (ret == true)
                this.Close();
            else
            {
                abc_app.msgbox("The transaction failed", "No data have been commited", Microsoft.VisualBasic.MsgBoxStyle.Critical);
                this.Close();
            }
        }

        private void street_no_TextChanged(object sender, EventArgs e)
        {
            TextBox TX = (TextBox)sender;
            abc_app.Validate_Numaric(ref TX);
        }

        private void street_no_Validating(object sender, CancelEventArgs e)
        {
            TextBox TX = (TextBox)sender;
            e.Cancel = !(abc_app.Validate_Numaric(ref TX));
        }

        private void dob_TextChanged(object sender, EventArgs e)
        {
           // abc_app.Validate_Date(sender);
        }

        private void dob_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = !(abc_app.Validate_Date (sender));
        }

        private void AddNewPatients_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
